package webrtc

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/pion/webrtc/v3"
)

func main() {

	// 创建pc， 并且指定stun服务器
	pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
		ICEServers: []webrtc.ICEServer{
			{
				URLs: []string{"stun:stun.l.google.com:19302"},
			},
		},
	})

	if err != nil {
		panic(err.Error())
	}

	sdp := webrtc.SessionDescription{}
	// todo:这边需要你自己设置对端的sdp信息
	sdpStr := "eyJ0eXBlIjoib2ZmZXIiLCJzZHAiOiJ2PTBcclxubz0tIDE2OTQ4Nzk1NDc5OTk0ODE3MDYgMiBJTiBJUDQgMTI3LjAuMC4xXHJcbnM9LVxyXG50PTAgMFxyXG5hPWdyb3VwOkJVTkRMRSAwXHJcbmE9ZXh0bWFwLWFsbG93LW1peGVkXHJcbmE9bXNpZC1zZW1hbnRpYzogV01TIE1PQ0xmd21HT1hwRG41dlB5R3lyZU5nclJ0Y2xQR2xCVmdjSlxyXG5tPXZpZGVvIDUzMzUxIFVEUC9UTFMvUlRQL1NBVlBGIDk2IDk3IDk4IDk5IDEwMCAxMDEgMTAyIDEyMSAxMjcgMTIwIDEyNSAxMDcgMTA4IDEwOSAzNSAzNiAxMjQgMTE5IDEyMyAxMTggMTE0IDExNSAxMTZcclxuYz1JTiBJUDQgNDcuOTEuMjE2LjE5XHJcbmE9cnRjcDo5IElOIElQNCAwLjAuMC4wXHJcbmE9Y2FuZGlkYXRlOjIwOTQwMjg4ODIgMSB1ZHAgMjEyMjI2MDIyMyAxMC4xMDYuMjEyLjc5IDUzMzUxIHR5cCBob3N0IGdlbmVyYXRpb24gMCBuZXR3b3JrLWlkIDEgbmV0d29yay1jb3N0IDEwXHJcbmE9Y2FuZGlkYXRlOjg0NDM0MjQzNCAxIHRjcCAxNTE4MjgwNDQ3IDEwLjEwNi4yMTIuNzkgOSB0eXAgaG9zdCB0Y3B0eXBlIGFjdGl2ZSBnZW5lcmF0aW9uIDAgbmV0d29yay1pZCAxIG5ldHdv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UgYXB0PTExNFxyXG5hPXJ0cG1hcDoxMTYgdWxwZmVjLzkwMDAwXHJcbmE9c3NyYy1ncm91cDpGSUQgMjY2MjI0MzgzNiAxMDM2NTM2NTlcclxuYT1zc3JjOjI2NjIyNDM4MzYgY25hbWU6STk3MEs2VDVSWGZHRkRxaFxyXG5hPXNzcmM6MjY2MjI0MzgzNiBtc2lkOk1PQ0xmd21HT1hwRG41dlB5R3lyZU5nclJ0Y2xQR2xCVmdjSiAzMTU2MTk3Ny04MzI3LTQ4MzUtYmNkYi05NTk0MGY0YTE5NWFcclxuYT1zc3JjOjI2NjIyNDM4MzYgbXNsYWJlbDpNT0NMZndtR09YcERuNXZQeUd5cmVOZ3JSdGNsUEdsQlZnY0pcclxuYT1zc3JjOjI2NjIyNDM4MzYgbGFiZWw6MzE1NjE5NzctODMyNy00ODM1LWJjZGItOTU5NDBmNGExOTVhXHJcbmE9c3NyYzoxMDM2NTM2NTkgY25hbWU6STk3MEs2VDVSWGZHRkRxaFxyXG5hPXNzcmM6MTAzNjUzNjU5IG1zaWQ6TU9DTGZ3bUdPWHBEbjV2UHlHeXJlTmdyUnRjbFBHbEJWZ2NKIDMxNTYxOTc3LTgzMjctNDgzNS1iY2RiLTk1OTQwZjRhMTk1YVxyXG5hPXNzcmM6MTAzNjUzNjU5IG1zbGFiZWw6TU9DTGZ3bUdPWHBEbjV2UHlHeXJlTmdyUnRjbFBHbEJWZ2NKXHJcbmE9c3NyYzoxMDM2NTM2NTkgbGFiZWw6MzE1NjE5NzctODMyNy00ODM1LWJjZGItOTU5NDBmNGExOTVhXHJcbiJ9"

	// 前端页面会对sdp进行base64的encode
	b, err := base64.StdEncoding.DecodeString(sdpStr)
	if err != nil {
		panic(err)
	}

	// 反序列化为sdp
	if err = json.Unmarshal(b, &sdp); err != nil {
		panic(err.Error())
	}

	// 设置远端的sdp
	if err = pc.SetRemoteDescription(sdp); err != nil {
		panic(err.Error())
	}

	// 创建协商结果
	answer, err := pc.CreateAnswer(nil)
	if err != nil {
		panic(err.Error())
	}

	// 设置自己的协商结果
	if err = pc.SetLocalDescription(answer); err != nil {
		panic(err)
	}

	// 等待ice结束
	gatherCmp := webrtc.GatheringCompletePromise(pc)
	<-gatherCmp

	// 将协商并且收集完candidate的answer，输出到控制台
	answerBytes, err := json.Marshal(*pc.LocalDescription())
	if err != nil {
		panic(err)
	}

	/*
	   //这段代码可以省略，也可以放开。他可以清楚的让你知道你是否推流成功
	   pc.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
	   	fmt.Println("one track = ", remote.PayloadType())
	   	rtpBuf := make([]byte, 1400)
	   	for {
	   		n, as, err := remote.Read(rtpBuf)
	   		fmt.Println("n = ", n, "   as = ", as, " err = ", err)
	   	}
	   })
	*/

	fmt.Println(base64.StdEncoding.EncodeToString(answerBytes))

	select {}
}
